#include "Aimbot.h"

Aimbot g_pAimbot;

void Aimbot::DropTarget()
{
	vFinal	   = SD_Tools::Vector();
	m_iTarget  = -1;
	JUNK_CODE;
	m_flDistance = 999999999.9f;
}

void Aimbot::Init()
{
	TargetBone = BONE_HEAD;
	vFinal = SD_Tools::Vector();
	JUNK_CODE;
	m_iTarget = -1;
	m_flDistance = 999999999.9f;
	JUNK_CODE;
	Active = false;
	Silent = false;
}

void Aimbot::Enable()
{
	Active = true;
}

void Aimbot::Disable()
{
	Active = false;
}

void Aimbot::Set( bool Active )
{
	this->Active = Active;
}

void Aimbot::SetBone( int Bone )
{
	TargetBone = Bone;
}

bool Aimbot::Compute( CUserCmd* cmd )
{
	if (!Active)
	{
		return false;
	}
	player_info_t pinfo;
	SD_Tools::Vector vPlayer;
	JUNK_CODE;

	if(!GameUtilities::HasGun())
		return false;
	DropTarget();

	int LocalPlayerIndex = g_pInterfaces.g_pEngine->GetLocalPlayer();
	Entity* pMe = g_pInterfaces.g_pEntList->GetClientEntity(LocalPlayerIndex);
	if (!pMe)
	{
		return false;
	}

	JUNK_CODE;
	if (pMe->iTeamNum() == TeamSpec)
	{
		return false;
	}

	CBaseCombatWeapon* pWeapon = GameUtilities::GetBaseCombatWeapon(pMe);

	if(!pWeapon || !pWeapon->IsReady())
	{
		return false;
	}

	SD_Tools::Vector m_Eye = pMe->GetEyePosition();

	GameUtilities::Predict(pMe, m_Eye, 3.f);

	int MaxEntities = g_pInterfaces.g_pEntList->GetHighestEntityIndex();

	for(int i = 1; i < MaxEntities; ++i)
	{
		Entity* baseEntity = g_pInterfaces.g_pEntList->GetClientEntity(i);

		if(i == LocalPlayerIndex || baseEntity == NULL || baseEntity->dormant() || baseEntity == pMe || *baseEntity->lifeState() != 0 /*LIFE_ALIVE*/ || !g_pInterfaces.g_pEngine->GetPlayerInfo(i, &pinfo) || *baseEntity->iTeamNum() == *pMe->iTeamNum() || !baseEntity->GetBone(TargetBone, &vPlayer) || !GameUtilities::IsVisible(m_Eye, vPlayer, pMe, baseEntity))
			continue;
		JUNK_CODE;
		float m_Dist = SD_Tools::MathHelper::DistanceVector(m_Eye, vPlayer);
		if(m_flDistance < m_Dist)
			continue;
		m_flDistance =  m_Dist;
		m_iTarget = i;
		vFinal = vPlayer;
		vFinal.z += 8.0f;
		GameUtilities::Predict(baseEntity, vFinal, 5.f);
	}
	if(m_iTarget == -1)
	{
		return false;
	}
	if(!Silent)
	{
		cmd->buttons |= IN_ATTACK;
		cmd->viewangles = GameUtilities::CreateAimAngle(m_Eye, vFinal);
		return true;
	}

	SD_Tools::Vector viewforward, viewright, viewup, aimforward, aimright, aimup;
	float forward = cmd->forwardmove;
	float right = cmd->sidemove;
	JUNK_CODE;
	float up = cmd->upmove;

	SD_Tools::MathHelper::AngleToVectors(cmd->viewangles, &viewforward, &viewright, &viewup);

	cmd->buttons |= IN_ATTACK;
	cmd->viewangles = GameUtilities::CreateAimAngle(m_Eye, vFinal);

	SD_Tools::MathHelper::AngleToVectors(cmd->viewangles, &aimforward, &aimright, &aimup); 

	SD_Tools::Vector vForwardNorm, vRightNorm, vUpNorm;        
	GameUtilities::AngleNormalize(viewforward, vForwardNorm);  
	JUNK_CODE;
	GameUtilities::AngleNormalize(viewright, vRightNorm); 
	GameUtilities::AngleNormalize(viewup, vUpNorm); 

	cmd->forwardmove = (aimforward.DotProduct(SD_Tools::MathHelper::MulVector(vForwardNorm, forward)) + aimforward.DotProduct(SD_Tools::MathHelper::MulVector(vRightNorm, right)) + aimforward.DotProduct(SD_Tools::MathHelper::MulVector(vUpNorm, up))); 
	cmd->sidemove = (aimright.DotProduct(SD_Tools::MathHelper::MulVector(vForwardNorm, forward)) + aimright.DotProduct(SD_Tools::MathHelper::MulVector(vRightNorm, right)) + aimright.DotProduct(SD_Tools::MathHelper::MulVector(vUpNorm, up))); 
	JUNK_CODE;
	cmd->upmove = (aimup.DotProduct(SD_Tools::MathHelper::MulVector(vForwardNorm, forward)) + aimup.DotProduct(SD_Tools::MathHelper::MulVector(vRightNorm, right)) + aimup.DotProduct(SD_Tools::MathHelper::MulVector(vUpNorm, up))); 
	return true;
}